package com.batchaps.classes;

import java.io.IOException;

import javax.microedition.amms.GlobalManager;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.batchaps.gameentity.VisibleGameEntity;
import com.batchaps.global.GameEngine;

public class MapWithCells extends VisibleGameEntity{
	private int _pX;
	private int _pY;
	private float _scale = 1.0f;
	private Cell[][] _cell;
	private int _cellWidth;
	private int _cellHeight;
	private int _nRows;
	private int _nCols;
	private int[][] _matrix;
	
	public MapWithCells(String mapName , int cellWidth , int cellHeight , int nrows  , int ncols, int[][] matrix) {
		// TODO Auto-generated constructor stub
		_nRows = nrows;
		_nCols = ncols;
		_cellWidth = cellWidth;
		_cellHeight = cellHeight;
		_matrix = matrix;
		_cell = new Cell[nrows][ncols];
		
		_width = cellWidth * ncols;
		_height = cellHeight * nrows;
		
		_pY = GameEngine.k_Screen_Width - _height;
		_pX = -_pY;
		_posX = 0;
		_posY = _pY;
		
		Image[] imgCell = new Image[1];
		try {
			imgCell[1] = Image.createImage(mapName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(int i = 0 ; i < nrows ; i++){
			for(int j = 0 ; j < ncols ; j++){
				_cell[i][j] = new Cell(imgCell, _matrix[i][j], cellWidth, cellHeight, _posX + j*cellWidth, _posY + j*cellHeight);
			}
		}
	}
	
	public void refreshMap(){
		_pY = GameEngine.k_Screen_Height - _height;
		_pX = -_pY;
		
		_posX = 0;
		_posY = _pY;
		for(int i = 0 ; i < _nRows ;i++){
			for(int j = 0 ; j < _nCols ;j++){
				_cell[i][j].set_posX(_posX);
				_cell[i][j].set_posY(_posY);
			}
		}
	}
	
	public void Draw(Graphics g) {
		// TODO Auto-generated method stub
		for(int i = 0 ; i < _nRows ;i++){
			for(int j = 0 ; j < _nCols ;j++){
				_cell[i][j].Draw(g);
			}
		}
	}
	
	public void Update() {
		// TODO Auto-generated method stub
		moveMap(GameEngine.Distance);
	}
	
	public void moveMap(int distance){
		if(_pX > distance){
			_pY += distance;
			_pX = -_pY;
			float moveX = 0;
			float moveY = distance;
			for(int i = 0 ; i < _nRows ;i++){
				for(int j = 0 ; j < _nCols ;j++){
					_cell[i][j].Move(moveX, moveY);
				}
			}
		}
	}
	
	public int getRowColX(int x , int y){
		x = -(int)_posX;
		return (y/_cellHeight);
	}
	
	public int getRowColY(int x , int y){
		x = -(int)_posX;
		return (x/_cellWidth);
	}
	
	public float rowColToPixelX(int cellRowColX , int cellRowColY){
		return _cell[cellRowColX][cellRowColY].get_posX();
	}
	
	public float rowColToPixelY(int cellRowColX , int cellRowColY){
		return _cell[cellRowColX][cellRowColY].get_posY();
	}
	
	public int getPosition(){
		return _pX;
	}
	
	public int get_pX() {
		return _pX;
	}

	public void set_pX(int _pX) {
		this._pX = _pX;
	}

	public int get_pY() {
		return _pY;
	}

	public void set_pY(int _pY) {
		this._pY = _pY;
	}

	public float get_scale() {
		return _scale;
	}

	public void set_scale(float _scale) {
		this._scale = _scale;
	}

	public Cell[][] get_cell() {
		return _cell;
	}

	public void set_cell(Cell[][] _cell) {
		this._cell = _cell;
	}

	public int get_cellWidth() {
		return _cellWidth;
	}

	public void set_cellWidth(int _cellWidth) {
		this._cellWidth = _cellWidth;
	}

	public int get_cellHeight() {
		return _cellHeight;
	}

	public void set_cellHeight(int _cellHeight) {
		this._cellHeight = _cellHeight;
	}

	public int get_nRows() {
		return _nRows;
	}

	public void set_nRows(int _nRows) {
		this._nRows = _nRows;
	}

	public int get_nCols() {
		return _nCols;
	}

	public void set_nCols(int _nCols) {
		this._nCols = _nCols;
	}

	public int[][] get_matrix() {
		return _matrix;
	}

	public void set_matrix(int[][] _matrix) {
		this._matrix = _matrix;
	}

}
